package com.brainshare.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.brainshare.an.domain.AnWechatUser;
import com.brainshare.common.constant.CacheConstants;
import com.brainshare.common.core.redis.RedisCache;
import com.brainshare.common.utils.StringUtils;
import com.brainshare.common.utils.spring.SpringUtils;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Iterator;

public class CommonUtils
{
    /**
     *  获取请求的IP地址， https://ip.useragentinfo.com/json?ip=117.136.12.79  IP地址归属地查询
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request)
    {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if  ("127.0.0.1".equals(ip)||ip=="127.0.0.1"||"0:0:0:0:0:0:0:1".equals(ip)||ip=="0:0:0:0:0:0:0:1") {
            try
            {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e)
            {
                e.printStackTrace();
            }
        }
        return ip;
    }

    public static AnWechatUser getAnWechatUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token))
        {
            throw new RuntimeException("token不能为空");
        }
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        Collection<String> collection = redisCache.keys(CacheConstants.WECHAT_USER_TOKEN + "*" + ":" + token);
        if (collection.size() > 0)
        {
            Iterator iterator = collection.iterator();
            while (iterator.hasNext())
            {
                String wechatUserToken = iterator.next().toString();
                if (wechatUserToken.contains(token))
                {
                    String jsonString = redisCache.getCacheObject(wechatUserToken);
                    AnWechatUser anWechatUser = JSON.parseObject(jsonString, AnWechatUser.class);
                    if (anWechatUser != null)
                    {
                        return anWechatUser;
                    }
                }
            }
            throw new RuntimeException("登录已失效！请重新登录！");
        } else
        {
            throw new RuntimeException("登录已失效！请重新登录！");
        }
    }

}
